package week2;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @author The Setting sun
 * @create 2021-09-16 14:23
 */
public class CalculatorTest {
    String symbol;//记录+-*/的符号
    String v1 = new String();//第一个值
    String v2 = new String();//第二个值
    String k = new String();//记录文本框的值然后赋给v1，v2

    public static void main(String[] args) {
        CalculatorTest calculator = new CalculatorTest();
        calculator.init();
    }

    public void init() {
        JFrame frame = new JFrame("简易计算器");

        JPanel panel1 = new JPanel();
        JTextField text = new JTextField(42);
        text.setEditable(false);

        panel1.add(text, BorderLayout.CENTER);

        //创建计算器面板
        JPanel panel2 = new JPanel(new GridLayout(4, 4, 15, 15));
        panel2.setSize(500, 400);
        //为每个按钮赋值
        String str = "123+456-789*=0C/";
        JButton[] buttons = new JButton[str.length()];
        for (int i = 0; i < buttons.length; i++) {
            buttons[i] = new JButton();
            buttons[i].setText(str.charAt(i) + "");
            buttons[i].setFont(new Font("微软雅黑", Font.BOLD,16));
            buttons[i].addActionListener(new ActionListener() {
                //监听
                @Override
                public void actionPerformed(ActionEvent e) {
                    JButton button = (JButton) e.getSource();
                    String bText = button.getText();//获取按钮上的值

                    //按下=给v2赋值并根据symbol计算再把结果设置为文本框的值
                    if (bText.equals("=")) {
                        text.setText(k);
                        v2 = text.getText();

                        if (symbol.equals("+")){
                            text.setText(Double.parseDouble(v1) + Double.parseDouble(v2) + "");
                        }

                        if (symbol.equals("-")){
                            text.setText(Double.parseDouble(v1) - Double.parseDouble(v2) + "");
                        }

                        if (symbol.equals("*")){
                            text.setText(Double.parseDouble(v1) * Double.parseDouble(v2) + "");
                        }

                        if (symbol.equals("/")){
                            text.setText(Double.parseDouble(v1) / Double.parseDouble(v2) + "");
                        }
                    } else if (bText.equals("+")) {
                        //按下+给symbol设置为+，并给v1赋值
                        symbol = "+";
                        v1 = text.getText();
                        k = "";
                    } else if (bText.equals("-")) {
                        //按下-给symbol设置为-，并给v1赋值
                        symbol = "-";
                        v1 = text.getText();
                        k = "";
                    } else if (bText.equals("*")) {
                        //按下*给symbol设置为*，并给v1赋值
                        symbol = "*";
                        v1 = text.getText();
                        k = "";
                    } else if (bText.equals("/")) {
                        //按下/给symbol设置为/，并给v1赋值
                        symbol = "/";
                        v1 = text.getText();
                        k = "";
                    }else if (bText.equals("C")){//这个按钮是重置文本框
                        k = "";
                        text.setText("");
                    } else {//给k赋值，并在文本框显示
                        k += bText;
                        text.setText(k);
                    }
                }
            });
            panel2.add(buttons[i]);
        }

        //窗口初始化
        frame.add(panel1, BorderLayout.NORTH);
        frame.add(panel2);
        frame.pack();
        frame.setSize(500, 450);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}


